﻿using System;
using System.IO;
using System.Text;
using SharpSqlAnalyzer.pp.output;
using SharpSqlAnalyzer.pp.para;
using SharpSqlAnalyzer.pp.para.styleenums;
using SharpSqlAnalyzer.pp.print;

namespace SharpSqlAnalyzer.pp.stmtformatter
{
	// Token: 0x020001FA RID: 506
	public class SqlFormatter
	{
		// Token: 0x060011CA RID: 4554 RVA: 0x003163CC File Offset: 0x003145CC
		public virtual string format(TGSharpSqlParser parser, GFmtOpt option)
		{
			MemoryStream memoryStream = new MemoryStream();
			StreamWriter streamWriter = new StreamWriter(memoryStream);
			IPrinter printer = PrinterFactory.createTextPrinter(streamWriter);
			FormatterFactory.createAllStmtsFormatter(option).beforeFormat(parser.sqlstatements);
			for (int i = 0; i < parser.sqlstatements.size(); i++)
			{
				TCustomSqlStatement sql = parser.sqlstatements.get(i);
				FormatterFactory.processStatement(option, sql);
			}
			if (parser.sqlstatements != null && parser.sqlstatements.size() > 0)
			{
				FormatterFactory.createAllStmtsFormatter(option).doFormat(parser.sqlstatements);
				FormatterFactory.createAllStmtsFormatter(option).afterFormat(parser.sqlstatements);
				if (FormatterFactory.OutputConfig == null)
				{
					printer.OutputConfig = OutputConfigFactory.getOutputConfig(option, parser.DbVendor);
				}
				else
				{
					printer.OutputConfig = FormatterFactory.OutputConfig;
				}
				printer.print(parser.sqlstatements.get(0).startToken.container);
			}
			FormatterFactory.clearAllObject(option.sessionId);
			streamWriter.Close();
			return Encoding.UTF8.GetString(memoryStream.ToArray());
		}

		// Token: 0x060011CB RID: 4555 RVA: 0x003164C8 File Offset: 0x003146C8
		public static void Main(string[] args)
		{
			GFmtOpt gfmtOpt = GFmtOptFactory.newInstance();
			gfmtOpt.linenumberEnabled = false;
			gfmtOpt.parametersStyle = TAlignStyle.AsStacked;
			gfmtOpt.parametersComma = TLinefeedsCommaOption.LfbeforeCommaWithSpace;
			gfmtOpt.beStyleFunctionRightBEOnNewline = false;
			gfmtOpt.outputFmt = GOutputFmt.ofhtml;
			TGSharpSqlParser tGSharpSqlParser = new TGSharpSqlParser(EDbVendor.dbvmssql);
			tGSharpSqlParser.sqlfilename = "C:\\1.txt";
			tGSharpSqlParser.parse();
			Console.WriteLine(new SqlFormatter().format(tGSharpSqlParser, gfmtOpt));
		}
	}
}
